import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';

/// 程序数据
class AppModel with ChangeNotifier {
  static const String _ACCESS_TOKEN = "accessToken";
  AppModel(this.prefs) {
    debugPrint('$this init');
  }
  final SharedPreferences prefs;
  final int _bindingStoreId = 2;
  int _currentStoreId;
  Map<String, String> _switchableStoreList;
  Map<String, String> _manageableStoreList;

  /// 默认店铺
  int get bindingStoreId => _bindingStoreId;

  /// 登录信息
  String get accessToken => prefs.getString(_ACCESS_TOKEN);
  set accessToken(String value) {
    prefs.setString(_ACCESS_TOKEN, value);
    notifyListeners();
  }

  /// 当前店铺
  int get currentStoreId => _currentStoreId ?? _bindingStoreId;
  set currentStoreId(int value) {
    _currentStoreId = value;
    notifyListeners();
  }

  /// 可切换店铺列表
  Map<String, String> get switchableStoreList => _switchableStoreList ?? {};
  set switchableStoreList(Map<String, String> value) {
    _switchableStoreList = value;
    notifyListeners();
  }

  /// 可管理店铺列表
  Map<String, String> get manageableStoreList => _manageableStoreList ?? {};
  set manageableStoreList(Map<String, String> value) {
    _manageableStoreList = value;
    notifyListeners();
  }

  /// 清除数据
  Future clear() async {
    _currentStoreId = null;
    _switchableStoreList = null;
    _manageableStoreList = null;
    await prefs.remove(_ACCESS_TOKEN);
    notifyListeners();
  }
}
